package com.zenithmind.article.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.article.pojo.domain.Tag;
import com.zenithmind.article.pojo.query.TagQuery;
import com.zenithmind.article.service.TagService;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 标签控制器
 * 遵循单一职责原则：专注于标签管理
 * 遵循依赖倒置原则：通过构造器注入依赖
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/tag")
@io.swagger.v3.oas.annotations.tags.Tag(name = "标签管理")
public class TagController {

    private final TagService tagService;

    // 构造器注入，遵循依赖倒置原则
    public TagController(TagService tagService) {
        this.tagService = tagService;
    }

    @Log(description = "查询所有标签", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "查询所有标签")
    @GetMapping("/listTags")
    public Result<List<Tag>> listTags(){
        log.info("查询所有标签");
        List<Tag> tags = tagService.list();
        return Result.success(tags);
    }

    @Log(description = "分页查询所有标签", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "分页查询所有标签")
    @GetMapping("/pageTag")
    public Result<PageResult<Tag>> pageTag(TagQuery tagQuery){
        PageResult<Tag> page = tagService.pageTag(tagQuery);
        return Result.success(page);
    }
    
    @Log(description = "添加标签或修改标签", operationType = OperationTypeEnum.UPDATE)
    @Operation(summary = "添加标签或修改标签")
    @PostMapping("/addOrUpdateTag")
    public Result<Tag> addOrUpdateTag(@RequestBody Tag tag){
        Tag tagList = tagService.addOrUpdateTag(tag);
        return Result.success(tagList);
    }

    @Log(description = "批量删除标签", operationType = OperationTypeEnum.DELETE)
    @Operation(summary = "批量删除标签")
    @DeleteMapping("/deleteTags")
    public Result<Void> deleteTags(@RequestBody List<String> tagIds){
        tagService.removeByIds(tagIds);
        return Result.success();
    }
    
    @Log(description = "根据id查询标签", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "根据id查询标签")
    @GetMapping("/getTagById/{id}")
    public Result<Tag> getTagById(@PathVariable String id){
        Tag tag = tagService.getById(id);
        return Result.success(tag);
    }

    @Log(description = "获取热门标签", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "获取热门标签")
    @GetMapping("/getHotTags")
    public Result<List<Map<String, Object>>> getHotTags(@RequestParam(required = false, defaultValue = "10") Integer limit) {
        log.info("获取热门标签，数量限制：{}", limit);
        List<Map<String, Object>> hotTags = tagService.getHotTags(limit);
        return Result.success(hotTags);
    }
}
